import vue from '@vitejs/plugin-vue';

/* Svg图片 */
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';

/* Antd和全局自定义组件库的自动引入 */
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import vitePluginImp from 'vite-plugin-imp';

/* 使用JSX */
import vuejsx from '@vitejs/plugin-vue-jsx';

/* g-zip打包 */
import viteCompression from 'vite-plugin-compression';

import AutoImport from 'unplugin-auto-import/vite';

/* 创建Vite的插件集合 */
export default function (Resolve: any): any[] {
	const plugins = [
		vue({
			script: { defineModel: true }
		}),

		// 默认`false`, 项目中已改为`true`, 可把代码中 `on: { click: xx }` 转成 `onClick: xxx`
		vuejsx({ transformOn: true }),

		AutoImport({
			imports: ['vue', 'vue-router'],
			eslintrc: {
				// 是否自动生成 eslint 规则，建议生成之后设置 false
				enabled: true,
				// 指定自动导入函数 eslint 规则的文件
				filepath: './.eslintrc-auto-import.json'
			},

			include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/, /\.md$/],
			dts: 'src/auto-imports.d.ts'
		}),

		createSvgIconsPlugin({
			// 指定需要缓存的图标文件夹
			iconDirs: [Resolve(process.cwd(), 'src/global-assets/svgs')],
			// 指定symbolId格式
			symbolId: 'icon-[dir]-[name]'
		}),

		Components({
			resolvers: [AntDesignVueResolver({ importLess: true, resolveIcons: true })],
			// 全局公共组件
			dirs: ['src/global-components'],
			include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
			extensions: ['jsx', 'vue'],
			deep: true
		}),

		vitePluginImp({
			libList: [
				{
					libName: 'ant-design-vue',
					style(name): string | boolean | string[] {
						return `ant-design-vue/es/${name}/style/index`;
					}
				}
			]
		})
	];

	if (process.env.NODE_ENV === 'production') {
		plugins.push(
			viteCompression({
				verbose: true,
				disable: false,
				threshold: 1024,
				algorithm: 'gzip',
				ext: '.gz'
			})
		);
	}

	return plugins;
}
